package com.jason.www;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jason.www.pojo.Movie;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.ReindexAction;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

/**
 * https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.4/index.html
 * _bulk 进行批量操作 和  _mget  复杂查询
 */
public class ElasticsearchTest2 {

    @Test
    public void batchSetIndex(){
        Movie movie1 = new Movie("The Godfather","Francis Ford Coppola",1972, Arrays.asList("Crime", "Drama"));
        Movie movie2 = new Movie("Lawrence of Arabia","David Lean",1962, Arrays.asList("Adventure", "Biography", "Drama"));
        Movie movie3 = new Movie("To Kill a Mockingbird","Robert Mulligan",1962, Arrays.asList("Crime", "Drama", "Mystery"));
        Movie movie4 = new Movie("Apocalypse Now","Francis Ford Coppola",1979, Arrays.asList("Drama", "War"));
        Movie movie5 = new Movie("Kill Bill: Vol. 1","Quentin Tarantino",2003, Arrays.asList("Action", "Crime", "Thriller"));
        Movie movie6 = new Movie("The Assassination of Jesse James by the Coward Robert Ford","Andrew Dominik",2007, Arrays.asList("Biography", "Crime", "Drama"));

        BulkRequestBuilder bulkRequest = ElasticSearchHelper.getElasticSearchClient().prepareBulk();
        bulkRequest.add(new IndexRequest("movies", "movie", "1").source(JSON.toJSONString(movie1), XContentType.JSON))
                .add(new IndexRequest("movies", "movie", "2").source(JSON.toJSONString(movie2), XContentType.JSON))
                .add(new IndexRequest("movies", "movie", "3").source(JSON.toJSONString(movie3), XContentType.JSON))
                .add(new IndexRequest("movies", "movie", "4").source(JSON.toJSONString(movie4), XContentType.JSON))
                .add(new IndexRequest("movies", "movie", "5").source(JSON.toJSONString(movie5), XContentType.JSON))
                .add(new IndexRequest("movies", "movie", "6").source(JSON.toJSONString(movie6), XContentType.JSON));
        BulkResponse resp = bulkRequest.execute().actionGet();
        if(resp.hasFailures()){
            System.out.println("批量插入失败");
        }
        System.out.println(resp.status());

    }

    @Test
    public void multiGetIndex(){
        MultiGetResponse responses = ElasticSearchHelper.getElasticSearchClient().prepareMultiGet()
                .add("actions" ,null,"1","2")
                .add("movies", "movie","3")
                .add("movies", "movie","4","5")
                .get();
        for (MultiGetItemResponse itemResponse : responses) {
            GetResponse response = itemResponse.getResponse();
            if (response.isExists()) {
                System.out.println(response.getSourceAsString());
                System.out.println("-----------------------------------");
            }
        }
    }

    @Test
    public void reindex(){
        BulkByScrollResponse response = ReindexAction.INSTANCE.newRequestBuilder(ElasticSearchHelper.getElasticSearchClient())
                .destination("movies")
                .filter(QueryBuilders.matchQuery("director", "Francis"))
                .get();
        List<BulkItemResponse.Failure> bulkFailures = response.getBulkFailures();
        for (BulkItemResponse.Failure bulkFailure : bulkFailures) {
            System.out.println(bulkFailure);
        }
    }


    @Test
    public void queryIndex(){
        SearchResponse searchResponse =ElasticSearchHelper.getElasticSearchClient().prepareSearch("movies")
                .setSearchType(SearchType.DEFAULT)
                //.setQuery(QueryBuilders.fuzzyQuery("title", "Kill"))//指定字段模糊搜索
                //.setQuery(QueryBuilders.queryStringQuery("Ford"))//全文模糊搜索
                //.setQuery(QueryBuilders.regexpQuery("director",".*"))//正则表达匹配搜索
                //.setQuery(QueryBuilders.multiMatchQuery("Ford","title","director"))
                .setQuery(QueryBuilders.rangeQuery("year").from(2000).to(2018))//Range查询
                .setFrom(0).setSize(3)//分页条件搜索
                .get();
        System.out.println(searchResponse);
        searchResponse.getHits().forEach(hit ->{
            Movie movie = JSONObject.parseObject(hit.getSourceAsString(), Movie.class);
            System.out.println(movie);
        });
    }




}
